package com.sdjz.club.club.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdjz.club.club.common.Result;
import com.sdjz.club.club.entity.WxUserInfo;
import com.sdjz.club.club.mapper.WxuserinfoMapper;
import com.sdjz.club.club.service.WxuserinfoService;
import com.sdjz.club.club.service.impl.WxuserinfoServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dftre
 * @since 2022-07-15
 */
@RestController
@RequestMapping("/club/wxuserinfo")
public class WxuserinfoController {
    @Autowired
    WxuserinfoService wxUserInfoService;

    @Autowired
    WxuserinfoServiceImp t_wxUserInfoService;

    @Autowired
    WxuserinfoMapper t_wxUserInfoMapper;


    @PostMapping("wxLogin")
    public Result wxLogin(@RequestBody WxUserInfo wxUserInfo) {
        return wxUserInfoService.Login(wxUserInfo);
    }

    //查询数据总条数
    @RequestMapping("total")
    public CommonResult selectLength() {
        return new CommonResult(t_wxUserInfoService.count());
    }


    //查询
    @GetMapping("/select")
    public CommonResult select(
            @RequestParam(name = "openid", required = false, defaultValue = "null") String openid
    ) {
        QueryWrapper<WxUserInfo> wrapper = new QueryWrapper<>();
        if (!openid.equals("null")) wrapper.eq("openid", openid);
        return new CommonResult(t_wxUserInfoMapper.selectList(wrapper));
    }

    // 实现分页查询
    @GetMapping("list")
    public CommonResult selectPage(
            @RequestParam(name = "current") int current,
            @RequestParam(name = "size", required = false, defaultValue = "20") int size
    ) {
        Page<WxUserInfo> page = new Page<>(current, size);
        t_wxUserInfoMapper.selectPage(page, null);
        return new CommonResult(page.getRecords());
    }

    // 实现删除
    @PostMapping("/delete")
    public CommonResult deleteMenu(@RequestParam("id") String id) {
        return CommonResult.succ(t_wxUserInfoMapper.deleteById(id));
    }

    // 实现插入
    @PostMapping("/insert")
    public CommonResult addMenu(@RequestBody WxUserInfo t_wxUserInfo) {
        return CommonResult.succ(t_wxUserInfoMapper.insert(t_wxUserInfo));
    }

    // 实现更新
    @PostMapping("/update")
    public CommonResult updateMenu(
            @RequestBody WxUserInfo t_wxUserInfo
    ) {
        QueryWrapper<WxUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", t_wxUserInfo.getOpenid());
        return CommonResult.succ(t_wxUserInfoMapper.update(t_wxUserInfo, wrapper));
    }
}

